package com.px.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.px.framework.mqtt.handler.IMqttHandler;
import com.px.plan.domain.PlanActionRecordMedia;
import com.px.system.domain.vo.PushMqVo;
import com.px.plan.mapper.PlanActionRecordMediaMapper;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import static com.px.framework.mqtt.model.TopicConst.*;

@Component
public class PlanMediaMqttHandler extends IMqttHandler {

    @Autowired
    private ObjectMapper mapper;


    @Autowired
    private PlanActionRecordMediaMapper planActionRecordMediaMapper;

    @Override
    public Pattern bindTopicName() {
        return Pattern.compile("^" + WRJ + SERVICES_ + REGEX_SN + _ACTION + "$");
    }

    //接收Osd定频数据
    @Override
    public void handler(String topic, MqttMessage mqttMessage) {
        PushMqVo pushMqVo = null;
        try {
            pushMqVo = mapper.readValue(mqttMessage.getPayload(), PushMqVo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
            /***
             * 设备上下线
             * 行动中的飞行器位置
             * 行动产生的图片视频
             */
            switch (pushMqVo.getType()){
                case SERVICES_DRONE_ADDRESS :

                    break;
                case SERVICES_DRONE_STORAGE :
                    /***
                     * 图片录像地址
                     */
                    PlanActionRecordMedia planActionRecordMedia = new PlanActionRecordMedia();
                    planActionRecordMedia.setDroneSn(pushMqVo.getSn());
                    planActionRecordMedia.setRecordId(pushMqVo.getData().getLong("RecordId"));
                    planActionRecordMedia.setMediaType(pushMqVo.getData().getInteger("mediaType"));
                    planActionRecordMedia.setMediaUrl(pushMqVo.getData().getString("mediaUrl"));
                    planActionRecordMedia.setMediaSize(pushMqVo.getData().getLongValue("mediaSize", 0L));
                    planActionRecordMedia.setLongitude(pushMqVo.getData().getBigDecimal("longitude"));
                    planActionRecordMedia.setLatitude(pushMqVo.getData().getBigDecimal("latitude"));
                    planActionRecordMedia.setHeight(pushMqVo.getData().getBigDecimal("height"));
                    planActionRecordMedia.setCreateTime(new Date(pushMqVo.getData().getLong("CreateTime")));

                    planActionRecordMediaMapper.insertPlanActionRecordMedia(planActionRecordMedia);
                    break;
                default:

                    break;
            }



    }
}
